﻿QueryString = {
    data: {},
    Initial: function () {
        var aPairs;
        var queryString = new String(window.location.search);
        queryString = queryString.substr(1, queryString.length); //remove   "?"     
        aPairs = queryString.split("&");
        for (var i = 0; i < aPairs.length; i++) {
            var aTmp = aPairs[i].split("=");
            if (aTmp.length <= 1)
                continue;
            var angularRouterPrifxIndex = aTmp[1].indexOf("#");
            if (angularRouterPrifxIndex >= 0) {
                aTmp[1] = aTmp[1].substring(0, angularRouterPrifxIndex);
            }
            this.data[aTmp[0]] = aTmp[1];
        }
    },
    GetValue: function (key) {
        return this.data[key];
    }
};

// document.getElementsByTagName('body')[0].style.marginTop = '20px';
// document.getElementsByTagName('body')[0].style.backgroundColor = "#ffffff";

var Storage = {};
Storage.dataStore = null;
Storage.initialize = function (flashElement) {
    this.dataStore = Storage.Cookies;
};
Storage.get = function (name, callback) {
    var value = this.dataStore.get(name, callback);
    if (value && callback) {
        callback(value);
    }
    else {
        return !!value ? $.parseJSON(value) : null;  // 这里的value必须以json形式传进来，否则会报错
    }
};
Storage.put = function (name, value, days, domain) {
    this.dataStore.put(name, value, days, domain);
};
Storage.remove = function (name) {
    this.dataStore.remove(name);
};

Storage.Cookies = {
    put: function (name, value,retireDate,domain) {
        var expiryDate = new Date();
        expiryDate.setTime(expiryDate.getTime() + 5 * 60 * 1000);
        if (retireDate){
            deadlinestr = "; expires=" + retireDate.toUTCString();
        } else {
            deadlinestr = "; expires=" + expiryDate.toUTCString();
        }
        // if (days == null || days == undefined) {
        //     days = deadline.getFullYear() + 10;
        //     deadline.setFullYear(days);
        // }
        // if (days) { deadlinestr = "; expires=" + deadline.toUTCString() } else {
        //     deadlinestr = '';
        // }

        var path = "/";
        var serializable = $.isArray(value) || $.isPlainObject(value);
        var storeValue = serializable ? JSON.stringify(value) : value;
        var domainstr = '';
        if (domain != location.host){
            domainstr = ((domain == null) ? ("; domain=.rongzi"+ location.hostname.substring(location.hostname.lastIndexOf('.'))) : ("; domain=" + domain));
        }
        document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(storeValue) + deadlinestr + ((path == null) ? "" : ("; path=" + path)) + domainstr
        //((domain == null) ? ("; domain=.rongzi"+ location.hostname.substring(location.hostname.lastIndexOf('.'))) : ("; domain=" + domain)); //+
        //((secure == true) ? "; secure" : "");
       
    },
    get: function (name) {
        try {
            var cookieFinder = new RegExp("(^|;) ?" + name + "=([^;]*)(;|$)");
            var cookie = document.cookie.match(cookieFinder);
            var value = "";
            if (cookie) {
                value = decodeURIComponent(cookie[2]);
            }
            return value;
        } catch (e) {
            return '';
        }        
    },
    remove: function (name) {
        var expiryDate = new Date();
        expiryDate.setTime(expiryDate.getTime() - 1);
        this.put({
            name: encodeURIComponent(name),
            expiry: expiryDate
        });
    }
}
Storage.LocalStorage = {
    put: function (name, value) {
        var serializable = $.isArray(value) || $.isPlainObject(value);
        var storeValue = serializable ? JSON.stringify(value) : value;
        localStorage.setItem(name, storeValue);
    },
    get: function (name) {
        var value = localStorage.getItem(name);
        return localStorage.getItem(name);
    },
    remove: function (name) {
        localStorage.removeItem(name);
    }
}
Storage.initialize();

// var retireDate = new Date();
// retireDate.setTime(retireDate.getTime() - 24 * 60 * 60 * 1000);
// Storage.put('userInfor','',retireDate);
// Storage.put('userInfor','',retireDate);
// Storage.put('userInfor','',retireDate,'m.rongzi.com');
// Storage.put('userInfor','',retireDate,'m.rongzi.cn');


//更行用户需求书
var UpdateApplyInfo = function ($http, $scope, UserID, CellPhoneNumber, InfoList, callback) {
    if (JSON.stringify($scope.applyInforTemp) == JSON.stringify($scope.applyInfor))
    {
        if (callback) {
            callback();
        }
        return;
    }
    $scope.dataChange = true;
    $scope.SaveAction = function (select) {
        if (select == 0) {
            $scope.dataChange = false;
            $scope.HasBack = false;
            return false;
        }
        //if (!confirm("数据已更改，是否保存？")) {
        //    if (callback) {
        //        callback();
        //    }
        //    return;
        //}

        //TODO：
        //alert("返回保存数据")
        postData.Content = { 'UserId': UserID, "CellPhoneNumber": CellPhoneNumber, Type: 0 };
        postData.Content.InfoList = InfoList;

        //$.post('/account/UpdateApplyInfo', JSON.stringify(postData), function (data) {
        //    if (data.Head.Ret == -1) {
        //        showErrorMsg(data.Head.Msg);
        //    }
        //    else if (callback) { callback($rootScope) }
        //});
        $http({
            url: '/account/UpdateApplyInfo',
            method: "POST",
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: postData
        }).success(function (data) {
            $scope.applyPosting = false;
            if (data.Head.Ret == -1) {
                showErrorMsg(data.Head.Msg);
            }
            if (callback) {
                callback();
                var myLoanMoney = { LoanAmount: $scope.applyInfor.LoanAmount ,LoanMoney: $scope.applyInfor.LoanAmount};
                var result = JSON.parse(Storage.Cookies.get("userInfor"));
                result.LoanMoney = myLoanMoney.LoanMoney;
                result.LoanAmount = myLoanMoney.LoanAmount;
                Storage.Cookies.put("userInfor", result);
            }
        });
        
    }
}
